package com.bajiao.admin.controller.order;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.system.vo.LoginUser;
import com.bajiao.entity.order.CouponOrder;
import com.bajiao.service.order.CouponOrderService;
import com.bajiao.vo.order.CouponOrderAdmin;
import com.bajiao.vo.order.OrderAdmin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 优惠券订单
 * @Author: FanJq
 * @Date:   2024-12-14
 * @Version: V1.0
 */
@Slf4j
@Api(tags="优惠券订单")
@RestController
@RequestMapping("/order/coupon")
public class CouponOrderController extends AdminBaseController<CouponOrder, CouponOrderService> {
	@Autowired
	private CouponOrderService couponOrderService;

	/**
	 * @description: 分页列表查询
	 * @author: FanJq
	 * @date:   2024-12-14
	 * @return
	 */
	@ApiOperation(value="优惠券订单-分页列表查询", notes="优惠券订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(String orderNo, String keywords, String title, Integer status, String createTimeBegin, String createTimeEnd) {
        startPage();
		List<CouponOrderAdmin> resultList = couponOrderService.getAdminList(orderNo, keywords, title, status, createTimeBegin, createTimeEnd);
        return Result.ok(getDataTable(resultList));
	}

	 @ApiOperation(value = "订单数据统计", notes = "订单数据统计")
	 @GetMapping(value = "/stat")
	 public Result<?> getOrderStat() {
		 Map<String, Object> result = couponOrderService.getOrderStat();
		 return Result.ok(result);
	 }

	/**
	 * @description: 添加
	 * @author: FanJq
	 * @date:   2024-12-14
	 * @param couponOrder
	 * @return
	 */
	@ApiOperation(value="优惠券订单-添加", notes="优惠券订单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CouponOrder couponOrder) {
		couponOrderService.save(couponOrder);
		return Result.ok("添加成功！");
	}

	/**
	 * @description: 编辑
	 * @author: FanJq
	 * @date:   2024-12-14
	 * @param couponOrder
	 * @return
	 */
	@ApiOperation(value="优惠券订单-编辑", notes="优惠券订单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CouponOrder couponOrder) {
		couponOrderService.updateById(couponOrder);
		return Result.ok("编辑成功!");
	}

	/**
	 * @description: 通过id删除
	 * @author: FanJq
	 * @date:   2024-12-14
	 * @param id
	 * @return
	 */
	@ApiOperation(value="优惠券订单-通过id删除", notes="优惠券订单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		couponOrderService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * @description: 批量删除
	 * @author: FanJq
	 * @date:   2024-12-14
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="优惠券订单-批量删除", notes="优惠券订单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.couponOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * @description: 通过id查询
	 * @author: FanJq
	 * @date:   2024-12-14
	 * @param id
	 * @return
	 */
	@ApiOperation(value="优惠券订单-通过id查询", notes="优惠券订单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CouponOrder couponOrder = couponOrderService.getById(id);
		return Result.ok(couponOrder);
	}

  /**
   * @description: 导出excel
   * @author: FanJq
   * @date:   2024-12-14
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(String orderNo, String keywords, String productTitle, Integer status, String createTimeBegin, String createTimeEnd, String fileName) {
	  LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
	  // Step.2 获取导出数据
	  List<CouponOrderAdmin> exportList = couponOrderService.getAdminList(orderNo, keywords, productTitle, status, createTimeBegin, createTimeEnd);
	  // Step.3 AutoPoi 导出Excel
	  ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
	  mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
	  mv.addObject(NormalExcelConstants.CLASS, CouponOrderAdmin.class);
	  mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
	  mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
	  return mv;
  }

  /**
   * @description: 通过excel导入数据
   * @author: FanJq
   * @date:   2024-12-14
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, CouponOrder.class);
  }

}
